package com.duomizhibo.phonelive.fragment;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.duomizhibo.phonelive.R;
import com.duomizhibo.phonelive.sharepreference.MySharePreferenceUtil;
import com.duomizhibo.phonelive.ui.ExtendedWebView;

import static android.webkit.WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE;


@SuppressLint("ValidFragment")
public class SimpleCardFragment extends Fragment implements View.OnClickListener {
    private String mTitle;


    ExtendedWebView order_wv;

    public static SimpleCardFragment getInstance(String title) {
        SimpleCardFragment sf = new SimpleCardFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    TextView text_ri;
    TextView text_zhou;
    TextView text_yue;
    TextView text_zong;
    String url;

    WebChromeClient chromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView webView, int i) {
            Log.e("进度", i + "");
            if (i == 100) {

            } else {

            }
        }
    };

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_simple_card, null);
        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);

        card_title_tv.setText(mTitle);
        order_wv = (ExtendedWebView) v.findViewById(R.id.order_wv);
        order_wv.clearCache(true);

//        v4Webview.setWebChromeClient(chromeClient);
        order_wv.setWebChromeClient(chromeClient);
        order_wv.getSettings().setUseWideViewPort(true);

        order_wv.getSettings().setLoadWithOverviewMode(true);
        order_wv.getSettings().setSavePassword(true);
        order_wv.getSettings().setSaveFormData(true);
        order_wv.getSettings().setJavaScriptEnabled(true);

        // enable navigator.geolocation
        order_wv.getSettings().setGeolocationEnabled(true);
        order_wv.getSettings().setGeolocationDatabasePath(
                "/data/data/org.itri.html5webview/databases/");

        order_wv.requestFocus();
        order_wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        // v4_webview.setScrollBarStyle(0);
        String ua = order_wv.getSettings().getUserAgentString();
        order_wv.getSettings().setUserAgentString(
                ua + "; " + "mingtang_android");
     /*   order_wv.addJavascriptInterface(new HongBaoWebActivity.JsInterfaces(this),
                "AndroidWebView")*/;
        order_wv.getSettings().setBuiltInZoomControls(true);
        order_wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        order_wv.getSettings().setDomStorageEnabled(true);

        order_wv.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            order_wv.getSettings().setMixedContentMode(
                    MIXED_CONTENT_COMPATIBILITY_MODE);
        }
        order_wv.getSettings().setAllowFileAccess(true);
        order_wv.getSettings().setAppCacheEnabled(true);
        order_wv.getSettings().setJavaScriptEnabled(true);
//        v4Webview.getSettings().setAppCachePath(appCachePath);
        order_wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        order_wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//		v4_webview.getSettings().setSupportZoom(true);
        order_wv.getSettings().setDefaultTextEncodingName("utf-8");
        order_wv.getSettings().setDisplayZoomControls(false);
        order_wv.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent ev) {

                ((android.webkit.WebView) v).requestDisallowInterceptTouchEvent(true);

                return false;
            }
        });


        text_ri = (TextView) v.findViewById(R.id.text_ri);
        text_zhou = (TextView) v.findViewById(R.id.text_zhou);
        text_yue = (TextView) v.findViewById(R.id.text_yue);
        text_zong = (TextView) v.findViewById(R.id.text_zong);
        text_ri.setOnClickListener(this);
        text_zhou.setOnClickListener(this);
        text_yue.setOnClickListener(this);
        text_zong.setOnClickListener(this);
        String mUid = (String) MySharePreferenceUtil.get(getActivity(), "id", "");

        if (mTitle.equals("收入榜")){
            url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=income&type=day";

        }else {
//            http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=income&type=xx

            url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=orderlist&type=day";
        }


//         url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=orderlist&type=day";

        order_wv.loadUrl(url);
        text_ri.setTextColor(Color.parseColor("#F9D648"));

        text_zhou.setTextColor(Color.parseColor("#AAAAAA"));
        text_yue.setTextColor(Color.parseColor("#AAAAAA"));
        text_zong.setTextColor(Color.parseColor("#AAAAAA"));
        text_ri.setBackground(getResources().getDrawable(R.color.white));
        text_zhou.setBackground(getResources().getDrawable(R.color.mygren));
        text_yue.setBackground(getResources().getDrawable(R.color.mygren));
        text_zong.setBackground(getResources().getDrawable(R.color.mygren));
        return v;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.text_ri:



                order_wv.clearHistory();


                if (mTitle.equals("收入榜")){
                    url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=income&type=day";
                }else {

                    url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=orderlist&type=day";
                }

                order_wv.loadUrl(url);
//                order_wv.reload();
                text_ri.setTextColor(Color.parseColor("#F9D648"));

                text_zhou.setTextColor(Color.parseColor("#AAAAAA"));
                text_yue.setTextColor(Color.parseColor("#AAAAAA"));
                text_zong.setTextColor(Color.parseColor("#AAAAAA"));
                text_ri.setBackground(getResources().getDrawable(R.color.white));
                text_zhou.setBackground(getResources().getDrawable(R.color.mygren));
                text_yue.setBackground(getResources().getDrawable(R.color.mygren));
                text_zong.setBackground(getResources().getDrawable(R.color.mygren));
                break;
            case R.id.text_zhou:
                order_wv.clearHistory();
                if (mTitle.equals("收入榜")){
                    url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=income&type=week";
                }else {

                    url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=orderlist&type=week";
                }


                order_wv.loadUrl(url);
//                order_wv.reload();
                text_zhou.setTextColor(Color.parseColor("#F9D648"));

                text_ri.setTextColor(Color.parseColor("#AAAAAA"));

                text_yue.setTextColor(Color.parseColor("#AAAAAA"));
                text_zong.setTextColor(Color.parseColor("#AAAAAA"));
                text_zhou.setBackground(getResources().getDrawable(R.color.white));
                text_ri.setBackground(getResources().getDrawable(R.color.mygren));
                text_yue.setBackground(getResources().getDrawable(R.color.mygren));
                text_zong.setBackground(getResources().getDrawable(R.color.mygren));
                break;
            case R.id.text_yue:
                order_wv.clearHistory();
                if (mTitle.equals("收入榜")){
                    url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=income&type=month";
                }else {

                    url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=orderlist&type=month";
                }

//                order_wv.loadUrl(url3);
                order_wv.loadUrl(url);
//                order_wv.reload();
                text_yue.setTextColor(Color.parseColor("#F9D648"));
                text_zhou.setTextColor(Color.parseColor("#AAAAAA"));

                text_ri.setTextColor(Color.parseColor("#AAAAAA"));

                text_zong.setTextColor(Color.parseColor("#AAAAAA"));
                text_yue.setBackground(getResources().getDrawable(R.color.white));
                text_ri.setBackground(getResources().getDrawable(R.color.mygren));
                text_zhou.setBackground(getResources().getDrawable(R.color.mygren));
                text_zong.setBackground(getResources().getDrawable(R.color.mygren));
                break;
            case R.id.text_zong:
                order_wv.clearHistory();
                if (mTitle.equals("收入榜")){
                    url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=income&type=all";

                }else {

                    url="http://apizb.meibo520.com/index.php?g=appapi&m=Contribute&a=orderlist&type=all";
                }

                 order_wv.loadUrl(url);
//                order_wv.reload();
                text_zong.setTextColor(Color.parseColor("#F9D648"));

                text_yue.setTextColor(Color.parseColor("#AAAAAA"));
                text_zhou.setTextColor(Color.parseColor("#AAAAAA"));

                text_ri.setTextColor(Color.parseColor("#AAAAAA"));


                text_zhou.setBackground(getResources().getDrawable(R.color.mygren));
                text_ri.setBackground(getResources().getDrawable(R.color.mygren));
                text_yue.setBackground(getResources().getDrawable(R.color.mygren));
                text_zong.setBackground(getResources().getDrawable(R.color.white));
                break;
        }
    }
}